$(function () {
   // 点击 菜品管理
   document.querySelector(".food").addEventListener("click", function () {
      users_back.style.display = 'none';
      document.querySelector(".page-content_user").style.display = 'none';
      document.querySelector(".page-content").style.display = "block";
   })
   var num = 1;
   var name = document.querySelectorAll(".name");
   var price = document.querySelectorAll(".price");
   var desc = document.querySelectorAll(".desc");
   var typename = document.querySelectorAll(".typename");
   var typeid = document.querySelectorAll(".typeid");
   var id = document.querySelectorAll(".id");
   $('.searchh').click(function () {
      $(".search").css("display", "block");
   })
   // 搜索 模糊查询 开始
   $(".search-btn").click(function () {
      if ($(".search-ipt").val() != '') {
         $.ajax({
            type: 'post',
            url: 'http://118.195.129.130:3000/food/getInfoByKw',
            data: {
               kw: $(".search-ipt").val()
            },
            success: function (data) {
               console.log(data.data);
               var tr = `<tr class="one">       
      <td><input type="checkbox" class="check" ><input type="text" class="name"></td>
      <td> <input type="text" class="price"></td>
      <td> <input type="text" class="desc"> </td>
      <td> <input type="text" class="typename"></td>
      <td> <input type="text" class="typeid"></td>
      <td class="id"></td>
      <td class="action">
          <span  class="edit">编辑</span>
          <span  class="die">删除</span>
      </td> 
        </tr>`;
               if (data.err === 0) {
                  var tbody = document.querySelector("tbody");
                  tbody.innerHTML = '';
                  for (let i = 0; i < data.data.length; i++) {
                     $('tbody').append(tr);
                  }
                  for (let i = 0; i < data.data.length; i++) {
                     var name = document.querySelectorAll(".name");
                     var price = document.querySelectorAll(".price");
                     var desc = document.querySelectorAll(".desc");
                     var typename = document.querySelectorAll(".typename");
                     var typeid = document.querySelectorAll(".typeid");
                     var id = document.querySelectorAll(".id");

                     price[i].value = data.data[i].price;
                     desc[i].value = data.data[i].desc;
                     typename[i].value = data.data[i].typename;
                     typeid[i].value = data.data[i].typeid;
                     id[i].innerHTML = data.data[i]._id;
                     name[i].value = data.data[i].name;
                  }

               } else {
                  console.log(data.msg);
               }
            },
            error: function (err) {
               console.log(err);
            }
         })
      } else {
         foodPage($('.currentPage').text());
      }
   })
   // 搜索 模糊查询 结束

   // 获取最大页数 开始
   $.ajax({
      type: 'get',
      url: 'http://118.195.129.130:3000/food/allpage',
      data: {
         Null: ''
      },
      success: function (data) {
         if (data.err === 0) {
            localStorage.setItem("pages", data.pages);
            localStorage.setItem("max_num", Math.ceil(data.pages / 4));
         } else {
            console.log(data.msg);
         }
      },
      error: function (err) {
         console.log(err);
      }
   })
   // 获取最大页数 结束

   // 修改 点击取消 
   $(".eno").click(function () {
      $(".editBox-back").css("display", "none");
   })
   // 点击编辑
   $(".edit").click(function () {
      $(this).parents("td").siblings().eq(5).addClass("_id");
      $(".editBox-back").css("display", "block");
   })
   // 修改 点击确定
   $(".eyes").click(function () {
      $.ajax({
         type: 'post',
         url: 'http://118.195.129.130:3000/food/update',
         data: {
            name: $(".ename").val(),
            price: $(".eprice").val(),
            desc: $(".edesc").val(),
            typename: $(".etypename").val(),
            typeid: $(".etypeid").val(),
            _id: $("._id").text()
         },
         success: function (data) {
            if (data.err === 0) {
               alert("修改成功！");
               console.log(data.msg);
               $("._id").removeClass("_id");
               $(".ename").val('');
               $(".eprice").val('');
               $(".edesc").val('');
               $(".etypename").val('');
               $(".etypeid").val('');
               $(".editBox-back").css("display", "none");
               foodPage($('.currentPage').text());
            } else {
               alert("修改失败！");
               console.log(data.msg);
               $("._id").removeClass("_id");
               $(".ename").val('');
               $(".eprice").val('');
               $(".edesc").val('');
               $(".etypename").val('');
               $(".etypeid").val('');
            }
         },
         error: function (err) {
            console.log(err);
         }
      })
   })
   //  分页查询
   function foodPage(num) {
      $.ajax({

         type: 'post',
         url: 'http://118.195.129.130:3000/food/getInfoByPage',
         data: {
            page: num,
            per_page: 4,
         },
         success: function (data) {
            if (data.err === 0) {
               for (let i = 0; i < 4; i++) {
                  name[i].value = '';
                  price[i].value = '';
                  desc[i].value = '';
                  typename[i].value = '';
                  typeid[i].value = '';
                  id[i].innerHTML = '';
               }
               for (let i = 0; i < data.data.length; i++) {
                  name[i].value = data.data[i].name;
                  price[i].value = data.data[i].price;
                  desc[i].value = data.data[i].desc;
                  typename[i].value = data.data[i].typename;
                  typeid[i].value = data.data[i].typeid;
                  id[i].innerHTML = data.data[i]._id;
               }
            } else {
               console.log(data.msg);
            }
         },
         error: function (err) {
            console.log(err);
         }
      })
   }
   foodPage($('.currentPage').text());
   // 分页查询结束
   // 左侧按钮 开始
   $(".p-l").click(function () {
      if (num > 1) {
         num--;
         foodPage(num);

      }
      $('.currentPage').text(num);
   })
   // 左侧按钮 结束

   // 右侧按钮 开始
   $(".p-r").click(function () {
      if (num < localStorage.getItem("max_num")) {
         num++;
         foodPage(num);
      }
      $('.currentPage').text(num);
   })
   // 右侧按钮 结束
   $(".a").html(`<i class="icon ion-android-person" style="font-size: 20px; line-height: 38px;color:white "></i>` +
      `欢迎您:   ` + localStorage.getItem('us') + `  管理员`);
   // 点击个人中心 页面更新
   $(".personal_information").click(function () {
      $(".page-content").css("display", "none");
      $(".users-back").css("display", "none");
      $(".page-content_user").css("display", "block");
   })
   $(".p_us_ipt").val(localStorage.getItem('us'));
   $(".p_id_ipt").attr("placeholder", localStorage.getItem('_id'));
   $(".p_phone_ipt").val(localStorage.getItem('phone'));
   $(".p_age_ipt").val(localStorage.getItem('age'));
   $(".p_sex_ipt").val(localStorage.getItem('sex'));
   // 点击确定 添加菜品
   $(".yes").click(function () {
      if (($(".aname").val() != '') & ($(".aprice").val() != '') & ($(".adesc").val() != '') & ($(".atypename").val() != '') & ($(".atypeid").val() != '')) {
         $.ajax({
            type: 'post',
            url: 'http://118.195.129.130:3000/food/add',
            data: {
               name: $(".aname").val(),
               price: $(".aprice").val(),
               desc: $(".adesc").val(),
               typename: $(".atypename").val(),
               typeid: $(".atypeid").val()
            },
            success: function (data) {
               if (data.err === 0) {
                  alert("添加成功！");
                  $(".aname").val('');
                  $(".aprice").val('');
                  $(".adesc").val('');
                  $(".atypename").val('');
                  $(".atypeid").val('');
                  $(".addBox-back").css("display", "none");
                  foodPage($('.currentPage').text());
               } else {
                  console.log(data.msg);
                  alert("添加失败！");
               }
            },
            error: function (err) {
               console.log(err);
            }
         })
      } else {
         alert("请输入完整！");
      }
   })
   // 修改用户信息
   $(".edit-user").click(function () {
      if ($(".p_us_ipt").val() == '' || $(".p_phone_ipt").val() == '' || $(".p_age_ipt").val() == '' || $(".p_sex_ipt").val() == '') {
         alert("请完善信息！");
      } else {
         $.ajax({
            type: 'post',
            url: 'http://118.195.129.130:3000/user/mod',
            data: {
               us: $(".p_us_ipt").val(),
               _id: $(".p_id_ipt").attr("placeholder"),
               phone: $(".p_phone_ipt").val(),
               age: $(".p_age_ipt").val(),
               sex: $(".p_sex_ipt").val()
            },
            success: function (data) {
               if (data.err === 0) {
                  console.log(data.msg);
                  alert("修改成功！");
                  localStorage.setItem("us", $(".p_us_ipt").val());
                  localStorage.setItem("_id", $(".p_id_ipt").attr("placeholder"));
                  localStorage.getItem('_id');
                  localStorage.setItem("sex", $(".p_sex_ipt").val());
                  localStorage.getItem('sex');
                  localStorage.setItem("age", $(".p_age_ipt").val());
                  localStorage.getItem('age');
                  localStorage.setItem("phone", $(".p_phone_ipt").val());
                  localStorage.getItem('phone');
                  $(".a").html(`<i class="icon ion-android-person" style="font-size: 20px; line-height: 38px;color:white "></i>` +
                     `欢迎您:   ` + localStorage.getItem('us') + `  管理员`);
                  setTimeout(function () {
                     $(".p_us_ipt").val(localStorage.getItem('us'));
                     $(".p_id_ipt").attr("placeholder", localStorage.getItem('_id'));
                     $(".p_phone_ipt").val(localStorage.getItem('phone'));
                     $(".p_age_ipt").val(localStorage.getItem('age'));
                     $(".p_sex_ipt").val(localStorage.getItem('sex'));
                  }, 2000);
                  setTimeout(function () {
                     $(".page-content").css("display", "block");
                     $(".page-content_user").css("display", "none");
                  }, 3000)
               } else {
                  console.log(data.msg);
               }
            },
            error: function (err) {
               console.log(err);
            }
         })
      }

   })
   // 用户退出
   $(".quit").click(function () {
      $.ajax({
         type: 'post',
         url: 'http://118.195.129.130:3000/user/out',
         data: {
            Null: ''
         },
         success: function (data) {
            if (data.err === 0) {
               alert("退出成功！");
               $(location).attr('href', '../html/login.html');
            } else {
               console.log(data.msg);
            }
         },
         error: function (err) {
            console.log(err);
         }
      })
   })
   // 面向对象
   var that;
   class Tab {
      constructor(id) {
         that = this;
         this.main = document.querySelector(id);
         this.add = document.querySelector('.add');
         this.tbody = this.main.querySelector('tbody');
         this.remove = document.querySelector('.remove');
         this.search = document.querySelector('.search-btn');
         this.modify = document.querySelector(".modify");
         this.init();
      }
      //  初始化操作
      init() {
         this.upNode();
         this.add.onclick = this.addTab;
         this.search.onclick = this.searchTab;
         this.modify.onclick = this.editTab;
         $('input').focus(function () {
            $(this).css("background-color", "skyblue");
         })
         $('input').blur(function () {
            $(this).css("background-color", "white");
         })
         //选中事件
         for (var i = 0; i < this.checks.length; i++) {
            this.checks[i].index = i;
            this.checks[i].onclick = this.addClass;
         }
         this.modify.onclick = this.editTab;
         //删除事件
         this.remove.onclick = this.removeTab;
      }
      addClass() {
         if ($(this).is(":checked")) {
            $(this).addClass("checked");

         } else if ($(this).attr("checked", "undefined")) {
            $(this).removeClass("checked");
         }

      }
      // 0.更新功能
      upNode() {
         this.options = this.main.querySelectorAll('.option');
         this.trs = this.main.querySelectorAll("tr");
         this.td = this.main.querySelectorAll("td");
         this.inputs = this.main.querySelectorAll("input");
         this.checks = document.querySelectorAll(".check");
         this.checkeds = document.querySelectorAll(".checked");
         this.determines = this.main.querySelectorAll(".determine");
      }
      //  1.添加功能
      addTab() {
         $(".addBox-back").css("display", "block");
         $(".no").click(function () {
            $(".addBox-back").css("display", "none");
         })

      }

      //  2.删除功能
      removeTab() {
         $(".check").css("display", "block");
         if ($(".check").is(":checked")) {
            var r = confirm("您确定要删除所选吗!");
            if (r == true) {
               $(".check").css("display", "none");
               //  删除菜品数据
               for (var i = 0; i < $(".checked").length; i++) {
                  $.ajax({
                     type: 'post',
                     url: 'http://118.195.129.130:3000/food/del',
                     data: {
                        _id: $(".checked").eq(i).parents("td").siblings().eq(4).text(),
                     },
                     success: function (data) {
                        if (data.err === 0) {
                           console.log(data.msg);
                           foodPage(num);
                        } else {
                           console.log(data.msg);
                        }
                     },
                     error: function (err) {
                        console.log(err);
                     }
                  })
               }
               $(".checked").parents("tr").find("input[type='text']").val('');
               $(".id").text('');
               $(".check").css("display", "none");
               $('.checked').removeAttr('checked');
            } else {

            }

         }
      }
   }
   new Tab("#table");
   // 分页功能
   if (localStorage.getItem("pages")) {
      foodPage(num);
   }




   // 用户管理
   var us_names = document.querySelectorAll(".us-name");
   var us_ages = document.querySelectorAll(".us-age");
   var us_sexs = document.querySelectorAll(".us-sex");
   var us_phones = document.querySelectorAll(".us-phone");
   var us_integrals = document.querySelectorAll(".us-integral");
   var us_ids = document.querySelectorAll(".us-id");
   var users_back = document.querySelector(".users-back");
   var users = document.querySelector(".users");
   var tds = document.querySelectorAll("td");
   var us_removes = document.querySelectorAll(".us-remove");

   function jumpUsers() {
      users_back.style.display = 'block';
   }
   users.addEventListener("click", jumpUsers);
   //  分页查询
   function usersPage(num) {
      $.ajax({
         type: 'post',
         url: 'http://118.195.129.130:3000/users/getInfoByPage_users',
         data: {
            page: num,
            per_page: 6
         },
         success: function (data) {
            if (data.err === 0) {
               for (let i = 0; i < 6; i++) {
                  us_names[i].innerHTML = '';
                  us_ages[i].innerHTML = '';
                  us_sexs[i].innerHTML = '';
                  us_phones[i].innerHTML = '';
                  us_integrals[i].innerHTML = '';
                  us_ids[i].innerHTML = '';
               }
               for (let i = 0; i < data.data.length; i++) {
                  us_names[i].innerHTML = data.data[i].us;
                  us_ages[i].innerHTML = data.data[i].age;
                  us_sexs[i].innerHTML = data.data[i].sex;
                  us_phones[i].innerHTML = data.data[i].phone;
                  us_integrals[i].innerHTML = data.data[i].integral;
                  us_ids[i].innerHTML = data.data[i]._id;
               }
            } else {
               console.log(data.msg);
            }
         },
         error: function (err) {
            console.log(err);
         }
      })
   }
   usersPage(1);
   //删除
   for (let i = 0; i < us_removes.length; i++) {
      us_removes[i].addEventListener("click", function () {

         this.parentNode.parentNode.children[5].classList.add("_id");
         $.ajax({
            type: 'post',
            url: 'http://118.195.129.130:3000/users/del_users',
            data: {
               _id: document.querySelector("._id").innerHTML
            },
            success: function (data) {
               if (data.err === 0) {
                  console.log(data.msg);
                  alert("删除成功!");
                  document.querySelector("._id").classList.remove("_id");
                  usersPage(1);
               } else {
                  console.log(data.msg);
               }
            },
            error: function (err) {
               console.log(err);
            }
         })
      })
   }
   // 左侧按钮 开始
   document.querySelector(".us-p-l").addEventListener("click", function () {
      if (num > 1) {
         num--;
         usersPage(num);
      }
      document.querySelector('.us-currentPage').innerHTML = num;
   })
   // 左侧按钮 结束

   // 右侧按钮 开始
   document.querySelector(".us-p-r").addEventListener("click", function () {
      if (num < localStorage.getItem("us-max_num")) {
         num++;
         usersPage(num);
      }
      document.querySelector('.us-currentPage').innerHTML = num;
   })
   // 右侧按钮 结束
   // 获取最大页数 开始
   $.ajax({
      type: 'get',
      url: 'http://118.195.129.130:3000/users/allpage_users',
      data: {
         Null: ''
      },
      success: function (data) {
         if (data.err === 0) {
            localStorage.setItem("us-pages", data.pages);
            localStorage.setItem("us-max_num", Math.ceil(data.pages / 6));
         } else {
            console.log(data.msg);
         }
      },
      error: function (err) {
         console.log(err);
      }
   })
   // 获取最大页数 结束
   // 修改用户 开始
   var us_edits = document.querySelectorAll(".us-edit")
   for (let i = 0; i < us_edits.length; i++) {
      us_edits[i].addEventListener("click", function () {
         document.querySelector(".us-editBox-back").style.display = 'block';
         this.parentNode.parentNode.children[5].classList.add("_id");
      })
   }
   // 修改 点击取消 
   document.querySelector(".us-eno").addEventListener("click", function () {
      document.querySelector(".us-editBox-back").style.display = 'none';
   })
   // 修改 点击确定
   document.querySelector(".us-eyes").addEventListener("click", function () {
      $.ajax({
         type: 'post',
         url: 'http://118.195.129.130:3000/users/update_users',
         data: {
            us: $(".us-ename").val(),
            age: $(".us-eprice").val(),
            sex: $(".us-edesc").val(),
            _id: $("._id").text()
         },
         success: function (data) {
            if (data.err === 0) {
               alert(data.msg);
               console.log(data.msg);
               $("._id").removeClass("_id");
               $(".us-ename").val('');
               $(".us-eprice").val('');
               $(".us-edesc").val('');
               $(".us-editBox-back").css("display", "none");
               usersPage($('.us-currentPage').text());
            } else {
               alert(data.msg);
               console.log(data.msg);
               $("._id").removeClass("_id");
               $(".us-ename").val('');
               $(".us-eprice").val('');
               $(".us-edesc").val('');
            }
         },
         error: function (err) {
            console.log(err);
         }
      })
   })
   // 修改用户 结束
   // 搜索 模糊查询 开始
   $(".us-search-btn").click(function () {
      if ($(".us-search-ipt").val() != '') {
         $.ajax({
            type: 'post',
            url: 'http://118.195.129.130:3000/users/getInfoByKw_users',
            data: {
               kw: $(".us-search-ipt").val()
            },
            success: function (data) {
               console.log(data.data);
               var tr = `  <tr>
            <td class="us-name"></td>
             <td class="us-age"></td>
             <td class="us-sex"></td>
            <td class="us-phone"></td>
            <td class="us-integral"></td>
            <td class="us-id"></td>
            <td class="us-action"><span class="us-remove">删除</span> <span class="us-edit">编辑</span> </td>
        </tr>`;
               if (data.err === 0) {
                  var us_tbody = document.querySelector(".us-tbody");
                  us_tbody.innerHTML = '';
                  for (let i = 0; i < data.data.length; i++) {
                     $('.us-tbody').append(tr);
                  }
                  for (let i = 0; i < data.data.length; i++) {
                     var us_name = document.querySelectorAll(".us-name");
                     var us_sex = document.querySelectorAll(".us-sex");
                     var us_age = document.querySelectorAll(".us-age");
                     var us_id = document.querySelectorAll(".us-id");
                     var us_phone = document.querySelectorAll(".us-phone");
                     var us_integral = document.querySelectorAll(".us-integral");
                     us_age[i].innerHTML = data.data[i].age;
                     us_sex[i].innerHTML = data.data[i].sex;
                     us_id[i].innerHTML = data.data[i]._id;
                     us_name[i].innerHTML = data.data[i].us;
                     us_phone[i].innerHTML = data.data[i].phone;
                     us_integral[i].innerHTML = data.data[i].integral;
                  }

               } else {
                  console.log(data.msg);
               }
            },
            error: function (err) {
               console.log(err);
            }
         })
      } else {
         usersPage($('.currentPage').text());
      }
   })
   // 搜索 模糊查询 结束
})